/**
 * <p>Title: com.szedi.common.util.StringUtil.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: col</p>
 */
package com.googlecode.cswish.util;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple util class
 * 
 * @author Jerry.Feng date 2005-7-9 13:42:26
 */
public class StringUtil {
	
	public static String[] split(String s, char c) {
		if (s == null) {
			return null;
		}
		
		int len = s.length();
		List<String> list = new ArrayList<String>();
		int begin = 0;
		for (int i = 0; i < len; i++) {
			if (s.charAt(i) == c) {
				list.add(s.substring(begin, i));
				begin = i + 1;
			}
		}
		if (begin < len) {
			list.add(s.substring(begin));
		}
		String[] array = new String[list.size()];
		list.toArray(array);
		return array;
	}
	
	public static String replaceSpecialChar(String value) {
		if (value == null) {
			return null;
		}

		char [] cs = value.toCharArray();
		char [] buff = new char[cs.length];
		
		for (int i = 0; i < cs.length; i++) {
			char c = cs[i];
			if (c == '[') {
				c = '<';
			} else if (c == ']') {
				c = '>';
			} else if (c == '.'){
				c = '*';
			}
			
			buff[i] = c;
		}
		
		return new String(buff);
	}

	public static String restoreSpecialChar(String value) {
		if (value == null) {
			return null;
		}

		char [] cs = value.toCharArray();
		char [] buff = new char[cs.length];
		
		for (int i = 0; i < cs.length; i++) {
			char c = cs[i];
			if (c == '<') {
				c = '[';
			} else if (c == '>') {
				c = ']';
			} else if (c == '*'){
				c = '.';
			}
			
			buff[i] = c;
		}
		
		return new String(buff);
	}
	
	public static String remove(String value, char c) {
		int len = value.length();
		StringBuffer sb = new StringBuffer(len);
		for (int i = 0; i < len; i++) {
			char tmp = value.charAt(i);
			if (tmp != c) {
				sb.append(tmp);
			}
		}
		return sb.toString();
	}
	
	public static String notNull(String str) {
		return str == null ? "" : str;
	}
	
	public static boolean equals(String s1, String s2) {
		return s1 == null && s2 == null || s1 != null && s1.equals(s2);
	}
}
